package com.lucien.admin.controller.system;

import com.lucien.common.dto.RegistBody;
import com.lucien.common.dto.UserUpdate;
import com.lucien.common.result.GlobalResult;
import com.lucien.common.utils.StringUtils;
import com.lucien.framework.exception.AllException;
import com.lucien.system.domain.SysUser;
import com.lucien.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Lucien
 * @Date 2021/4/28 18:39
 * @Version 1.0
 * @Description:
 */
@RestController
@Api(tags = "用户接口")
@RequestMapping("/sysuser")
public class SysUserController extends AllException {

    @Autowired
    private SysUserService userService;


    @PostMapping("/regist")
    @ApiOperation(value = "用户注册")
    public GlobalResult regist(@RequestBody RegistBody registBody){
        int result = userService.userRegistere(registBody);
        if (result == 1){
            return GlobalResult.success("注册成功",result);
        }else if (result == -1){
            return GlobalResult.error("注册失败",result);
        }else if (result == -2){
            return GlobalResult.error("该用户名已注册", result);
        }else {
            return GlobalResult.error("注册失败",result);
        }
    }

    @GetMapping("/getbyname")
    @ApiOperation(value = "用户名查询")
    public GlobalResult selectByUsername(String username){
        SysUser result = userService.selectByUsername(username);
        if (result != null){
            return GlobalResult.success(result);
        }else {
            return GlobalResult.error();
        }
    }

    @GetMapping("/{userId}")
    @ApiOperation(value = "用户ID查询")
    public GlobalResult selectByUserId(@PathVariable("userId") Integer userId){
        SysUser result = userService.selectByUserId(userId);
        if (StringUtils.isNotNull(result)){
            return GlobalResult.success("查询成功", result);
        }else {
            return GlobalResult.error("查询失败", -1);
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "用户信息修改")
    public GlobalResult updateUser(@RequestBody UserUpdate userUpdate){
        Integer result = userService.updateUser(userUpdate);
        if (result == 1){
            return GlobalResult.success(result);
        }else {
            return GlobalResult.error();
        }
    }

    @RequiresRoles(value = {"admin"})
    @GetMapping("/disableUser/{userId}")
    @ApiOperation(value = "禁用用户")
    public GlobalResult disableUser(@PathVariable Integer userId){
        Integer result = userService.disableUser(userId);
        if (result == 1){
            return GlobalResult.success(result);
        }else {
            return GlobalResult.error();
        }
    }

    @RequiresRoles(value = {"admin"})
    @GetMapping("/enableuser/{userId}")
    @ApiOperation(value = "启用用户")
    public GlobalResult enableUser(@PathVariable Integer userId){
        Integer result = userService.enableUser(userId);
        if (result == 1){
            return GlobalResult.success(result);
        }else {
            return GlobalResult.error();
        }
    }

    @PostMapping("/updatepwd")
    @ApiOperation(value = "更新用户密码")
    public GlobalResult updatePwd(@RequestParam(name = "password", required = true) String password){
        int result = userService.updatePassword(password);
        if (result == 1){
            return GlobalResult.success("修改密码成功", result);
        }else {
            return GlobalResult.error("修改失败！！！", result);
        }
    }
}

